package com.cwx.web;

import com.alibaba.fastjson.JSON;
import com.cwx.common.AjaxResult;
import com.cwx.common.SystemEnums;
import com.cwx.common.Vector;
import com.cwx.dao.entity.OrderInfo;
import com.cwx.service.OrderInfoService;
import com.cwx.tools.TB;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/tb")
public class TBDetailsController {

    @Autowired
    private OrderInfoService orderInfoService;

    @GetMapping("/qrcode")
    public AjaxResult loginQRCode() {
        try {
            Vector vector = TB.getQrCode();
            return new AjaxResult().setMessage(vector.getMsg()).setData(vector.getData());
        } catch (IOException e) {
            e.printStackTrace();
            return new AjaxResult(false).setMessage("connect exception");
        }
    }

    @GetMapping("/checkScan")
    public AjaxResult checkSan(String lgToken,String orderId) {
        try {
            Vector vector1 = TB.scanStatus(lgToken);
            Integer code = vector1.getCode();
            if (code.equals(SystemEnums.SUCCESS.ordinal())) {
                new Thread(()->{
                    String cookie = (String) vector1.getData();
                    log.info("get details....");
                    Map<String, Object> tbmap = new HashMap<>();
                    try {
                        tbmap.put("basicInfo", JSON.parse(TB.getBasicInfo(cookie)));
                    } catch (IOException e) {
                        log.info("taobao basicinfo io exp:{}",e.getMessage());
                        tbmap.put("basicInfo", null);
                    }
                    try {
                        tbmap.put("alipayInfo", JSON.parse(TB.getAlipayInfo(cookie)));
                    } catch (IOException e) {
                        log.info("taobao alipayInfo io exp:{}",e.getMessage());
                        tbmap.put("alipayInfo", null);
                    }
                    try {
                        tbmap.put("orderInfo", JSON.parse(TB.getOrderInfo(cookie)));
                    } catch (IOException e) {
                        log.info("taobao orderInfo io exp:{}",e.getMessage());
                        tbmap.put("orderInfo", null);
                    }
                    try {
                        tbmap.put("ReceiptAddress", JSON.parse(TB.getReceiptAddress(cookie)));
                    } catch (IOException e) {
                        log.info("taobao ReceiptAddress io exp:{}",e.getMessage());
                        tbmap.put("ReceiptAddress", null);
                    }
                    String s = JSON.toJSONString(tbmap);
                    log.info(s);
                    log.info("Get tb data,next save data....");
                    // persistence
                    OrderInfo orderInfo = orderInfoService.findById(Long.parseLong(orderId));
                    orderInfo.setTbData(JSON.toJSONString(tbmap));
                    orderInfoService.save(orderInfo);
                    log.info("tb data persistence finished");
                }).start();
                return new AjaxResult().setMessage(vector1.getMsg());
            } else {
                return new AjaxResult(false).setMessage(vector1.getMsg());
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}